<?php
/**
 * 收货地址
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 14:50
 */

namespace app\api\controller;


use app\api\business\AddressBus;
use app\api\validate\Address as AddressValidate;

class Address extends UserCommon
{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 列表
     * @return \think\response\Json
     */
    public function getList(){
        $where = ['user_id' => $this->user_id];
        $list = (new AddressBus())->getList($where);
        return resJson(200,'ok',$list);
    }

    /**
     * 详情
     * @return \think\response\Json
     */
    public function get(){
        $id = input('param.id',0,'intval');
        if (!$id) return resJson(0,'参数错误');
        $data = (new AddressBus())->get($id);
        return resJson(200,'ok',$data);
    }

    /**
     * 新增收货地址
     * @return \think\response\Json
     */
    public function add(){
        $data = input('post.','','trim');
        $validate = new AddressValidate();
        $data['user_id'] = $this->user_id;
        if (!$validate->check($data)){
            return resJson(0,$validate->getError());
        }
        $res = (new AddressBus())->add($data);
        if ($res){
            return resJson(200,'添加成功');
        }else{
            return resJson(0,'添加失败');
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function delete(){
        $id = input('param.id',0,'intval');
        if (!$id) return resJson(0,'参数错误');
        $res = (new AddressBus())->delete($id);
        if ($res){
            return resJson(200,'操作成功');
        }else{
            return resJson(0,'操作失败');
        }
    }

    /**
     * 编辑收货地址
     * @return \think\response\Json
     */
    public function edit(){
        $data = input('post.','','trim');
        if (!$data['id']) return resJson(0,'参数错误');
        $validate = new AddressValidate();
        if (!$validate->check($data)){
            return resJson(0,$validate->getError());
        }
        $res = (new AddressBus())->save($data);
        if ($res){
            return resJson(200,'操作成功');
        }else{
            return resJson(0,'操作失败');
        }
    }

    /**
     * 设置默认地址
     */
    public function setDefault()
    {
        $id = input('param.id',0,'intval');
        if (!$id) return resJson(0,'参数错误');
        (new AddressBus())->setDefault($id);
        return resJson(200,'操作成功');
    }


}